# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     app
   Description :
   Author :       Flyoung
   date：          2023/9/7
-------------------------------------------------
   Change Activity:
                   2023/9/7:
-------------------------------------------------
"""

from flask import Flask, request
from flask_cors import CORS
from analyse.kmeans import kmeans_cluster
import sys
import os

from analyse.predict_model.predict import job_predict

current_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(current_path, '..'))

from analyse.score_model.preict import predict_web
from dao import db_url, db
from dao.query import (
    job_query,
    job_page_query,
    city_job_proportion_query,
    welfare_job_proportion_query,
    tech_job_proportion_query,
    job_query_by_name,
    salary_compare
)

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = db_url
db.init_app(app)
CORS(app, supports_credentials=True)


def success_result(data, msg='查询成功'):
    return {
        'code': 200,
        'data': data,
        'msg': msg
    }


def error_result(msg='请求参数有无或服务器程序出错'):
    return {
        'code': 500,
        'msg': msg
    }


@app.route('/')
def index():
    return '行业大数据'


@app.route('/job/<job_id>')
def job_detail(job_id):
    """
    查询工作详情
    :param job_id:
    :return:
    """
    try:
        return success_result(job_query(job_id))
    except:
        return error_result()


@app.route('/job/page', methods=['POST'])
def job_paging():
    """
    分页查询工作信息
    :return:
    """
    try:
        request_data = request.get_json()
        return success_result(job_page_query(request_data['page'], request_data['pageSize']))
    except:
        return error_result()


@app.route('/job/page/name', methods=['POST'])
def job_paging_by_name():
    """
    根据工作名分页查询工作信息
    :return:
    """
    try:
        request_data = request.get_json()
        return success_result(
            job_query_by_name(request_data['name'], request_data['page'], request_data['pageSize'])
        )
    except:
        return error_result()


@app.route('/job/proportion/<proportion_type>')
def city_job_proportion(proportion_type):
    """
    查询 城市、福利、技术 出现的频次
    :param proportion_type:
    :return:
    """
    if proportion_type == 'city':
        return success_result(city_job_proportion_query())
    elif proportion_type == 'welfare':
        return success_result(welfare_job_proportion_query())
    elif proportion_type == 'tech':
        return success_result(tech_job_proportion_query())
    else:
        return error_result()


@app.route('/job/score', methods=['POST'])
def score():
    """
    对工作进行评分
    :return:
    """
    try:
        request_data = request.get_json()
        return success_result({'score': predict_web(request_data)})
    except:
        return error_result()


@app.route('/job/kmeans', methods=['GET'])
def kemans():
    """
    对数据进行聚类分析
    :return:
    """
    try:
        return success_result({'img_base64': kmeans_cluster()})
    except:
        return error_result()


@app.route('/job/salary', methods=['POST'])
def salary_analyze():
    """
    工资对比
    :return:
    """
    try:
        request_data = request.get_json()
        return success_result(salary_compare(request_data['jobCount']))
    except:
        return error_result()


@app.route('/job/predict', methods=['POST'])
def user_job_predict():
    try:
        return success_result(job_predict(request.get_json()))
    except:
        return error_result()


if __name__ == '__main__':
    app.run(port='9243', debug=True, host='0.0.0.0')
